package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.merchant.MerchantEntity;
import com.dragon.modules.yuegou.entity.merchant.QrcodeEntity;
import com.dragon.modules.yuegou.mapper.merchant.QrcodeMapper;
import com.dragon.modules.yuegou.model.merchant.Qrcode;
import com.dragon.modules.yuegou.query.merchant.QrcodeQuery;
import com.dragon.modules.yuegou.service.merchant.MerchantService;
import com.dragon.modules.yuegou.service.merchant.QrcodeService;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商户二维码 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-28 14:32:38
 * @version 2.0
 */
@Slf4j
@Service("merQrcodeService")
public class QrcodeServiceImpl
		extends ServiceImpl<QrcodeMapper, QrcodeEntity>
		implements QrcodeService {

	@Autowired
	private MerchantService merchantService;

	@Override
	public void saveQrcode(String merchantId){
		Check.notNull(merchantId,"商户id不能为空");
		log.info("新增商户信息，新增商户二维码信息开始==，商户id={}", merchantId);
		Qrcode qrcode = new Qrcode();
		qrcode.setMerchantId(merchantId);
		save(qrcode);
		log.info("新增商户信息，新增商户二维码信息成功，商户id={}", merchantId);
	}

	@Override
	public Qrcode getQrcode(String merchantId) {
		Check.notNull(merchantId,"商户id不能为空");
		QrcodeQuery query = new QrcodeQuery();
		query.merchantId().eq(merchantId);
		query.status().eq(true);

		QrcodeEntity entity = getOne(query.toQueryWrapper());
		if (null != entity){
			Qrcode qrcode = new Qrcode(entity);
			MerchantEntity merchant = merchantService.getById(merchantId);
			if (null != merchant){
				qrcode.setMerchantName(merchant.getMerchantName());
			}
			return qrcode;
		}
		return new Qrcode();
	}
}